﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace IT_Pro.Class
{
    class NV_NhanVien
    {
        private string _maNV;

        public string MaNV
        {
            get { return _maNV; }
            set { _maNV = value; }
        }
        private string _tenNV;

        public string TenNV
        {
            get { return _tenNV; }
            set { _tenNV = value; }
        }
        private string _dienThoaiNB;

        public string DienThoaiNB
        {
            get { return _dienThoaiNB; }
            set { _dienThoaiNB = value; }
        }
        private string _dienThoaiDD;

        public string DienThoaiDD
        {
            get { return _dienThoaiDD; }
            set { _dienThoaiDD = value; }
        }
        private string _mail;

        public string Mail
        {
            get { return _mail; }
            set { _mail = value; }
        }
        private string _tenPhong;

        public string TenPhong
        {
            get { return _tenPhong; }
            set { _tenPhong = value; }
        }
        private string _taiKhoan;

        public string TaiKhoan
        {
            get { return _taiKhoan; }
            set { _taiKhoan = value; }
        }
        private string _matKhau;

        public string MatKhau
        {
            get { return _matKhau; }
            set { _matKhau = value; }
        }

        public static DataTable HienThiDsNhanVien()
        {
            string strSQL = "select * from NV_NhanVien where TrangThai='1'";
           return DataServices.ExecQuery(strSQL);
        }       
        public static DataTable HienThiDsNhanVienSearch(string keySearch, string fill, int searchMode )
        {
            string strSQL;
            if (searchMode == 1)
            {
                strSQL = "select * from NV_NhanVien nv,NV_PhongBan pb where nv.PHG=pb.MaPhong and " + fill + "='" + keySearch + "'";
            }
            else if (searchMode == 0)
            {
                strSQL = "select * from NV_NhanVien nv,NV_PhongBan pb where nv.PHG=pb.MaPhong and " + fill + " like '%" + keySearch + "%'";
            }
            else
            {
                strSQL = "select * from NV_NhanVien nv,NV_PhongBan pb where nv.PHG=pb.MaPhong";
            }
            return DataServices.ExecQuery(strSQL);
        }

        public static bool ThemNhanVien(NV_NhanVien _nhanvien)
        {
            string sql = "select * from NV_NhanVien where MaNV='" + _nhanvien.MaNV + "'";
            DataTable dt = DataServices.ExecQuery(sql);
            if (dt.Rows.Count > 0)
            {
                return false;
            }
            sql = "insert into NV_NhanVien values ('" + _nhanvien.MaNV + "',N'" + _nhanvien.TenNV + "','" + _nhanvien.DienThoaiNB + "','" + _nhanvien.DienThoaiDD + "','" + _nhanvien.Mail + "','" + _nhanvien.TenPhong + "','1','" + _nhanvien.TaiKhoan + "','" + _nhanvien.MatKhau + "')";
            DataServices.ExecNonQuery(sql);
            return true;
        }

        public static bool XoaNhanVien(string maNhanVien)
        {
            string strSQL = "delete NV_NhanVien where MaNV='" + maNhanVien + "'";
            DataServices.ExecNonQuery(strSQL);
            return true;
        }

        public static bool CapNhatNhanVien(NV_NhanVien _nhanvien)
        {
            string strSQL = "update NV_NhanVien set TenNV=N'" + _nhanvien.TenNV + "',DienThoaiNB='" + _nhanvien.DienThoaiNB + "',DienThoaiDD='" + _nhanvien.DienThoaiDD + "',Mail='" + _nhanvien.Mail + "',PHG='" + _nhanvien.TenPhong + "',TaiKhoan='" + _nhanvien.TaiKhoan + "',MatKhau='"+_nhanvien.MatKhau+"' where MaNV='" + _nhanvien.MaNV + "'";
            DataServices.ExecNonQuery(strSQL);
            return true;
        }

        public static int KiemTraNhanVien(string nv)
        {
            string strSQL = "select * from NV_NhanVien where MaNV='"+nv+"'";
            DataTable dt = DataServices.ExecQuery(strSQL);
            if (dt.Rows.Count > 0)
            {
                return 1;
            }
            else
            {
                return 0;
            }
        }

        public static bool ChuyenTrangThaiNhanVien(string maNhanvien,string trangthai)
        {
            string strSQL = "update NV_NhanVien set TrangThai='" + trangthai + "' where MaNV='" + maNhanvien + "'";
            DataServices.ExecNonQuery(strSQL);
            if (trangthai == "False")
            {
                strSQL = "update NV_CodeDienThoai set NgayHuy='" + DateTime.Today + "' where MaNV='" + maNhanvien + "'";
            }
            else
            {
                strSQL = "update NV_CodeDienThoai set NgayHuy=null where MaNV='" + maNhanvien + "'";
            }
            DataServices.ExecNonQuery(strSQL);
            return true;
        }

        public static bool ChuyenPhongBan(string maNhanvien, string phong)
        {
            string strSQL = "update NV_NhanVien set PHG='" + phong + "' where MaNV='" + maNhanvien + "'";
            DataServices.ExecNonQuery(strSQL);
            return true;
        }

        public static bool kiemtraNhanvienchuabangiaomay(string manv)
        {
            string strSQL = "select *  from CV_BanGiaoThietBi where NguoiNhan='" + manv + "' and Giu=1";
            DataTable dt = DataServices.ExecQuery(strSQL);
            if (dt.Rows.Count > 0)
            {
                return false;
            }
            return true;
        }
    }   
}
